package com.example.yyg.springframework.core.io;

import com.example.yyg.springframework.util.ClassUtils;
import org.springframework.util.Assert;

import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @Author: 冰岩
 * @Date: 2023/06/26
 * @Description:
 */
public class ClassPathResource implements Resource{

    String path;

    ClassLoader classLoader;

    public ClassPathResource(String path) {
        this(path, (ClassLoader)null);
    }

    public ClassPathResource(String path, ClassLoader classLoader) {
        Assert.notNull(path,"path can not be null");
        this.classLoader = classLoader != null ? classLoader: ClassUtils.getDefaultClassLoader();
        this.path = path;
    }

    @Override
    public InputStream getInputStream() throws FileNotFoundException {
        //todo.yyg
        InputStream is =  classLoader.getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("path not find:" + path);
        }
        return is;
    }
}
